package com.changpay.zywy.adapter

import android.content.Context
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.changpay.zywy.R
import com.changpay.zywy.bean.MyShopBean
import com.changpay.zywy.wight.CustomCircleProgressBar
import java.text.DecimalFormat

/**
 * Created by chenxz on 2018/4/22.
 */
class MyShopAdapter(private val context: Context?, datas: MutableList<MyShopBean.DataBean>) :
    BaseQuickAdapter<MyShopBean.DataBean, BaseViewHolder>(R.layout.item_my_shop, datas) {
    override fun convert(helper: BaseViewHolder?, item: MyShopBean.DataBean?) {
        item ?: return
        helper ?: return
        helper.setText(R.id.tv_shop_name, item.merchant_name)
            .setText(R.id.tv_shop_name, item.merchant_name)
            .setText(R.id.tv_sn_number, item.sn_code)
            .setText(R.id.tv_name, item.goods_name)
            .setText(R.id.tv_time, item.actication_time)
            .setText(R.id.tv_time, item.actication_time)
            .setText(R.id.tv_total_jiao_yi, "¥ ${item.trading_amount}")

        val lg_av_amount = item.lg_av_amount.toDouble()
        val target_price = item.target_price.toDouble()
        val lineCard_amout = item.lineCard_amout.toDouble()

        formatDouble(helper.getView(R.id.tv_money), lg_av_amount)
        val fenshu = lineCard_amout / target_price * 100

        val fenmu = if (0 < fenshu && fenshu < 1) {
            1
        } else {
            fenshu.toInt()
        }
        if (fenmu >= 100) {
            helper.setVisible(R.id.iv_yifan, true).setVisible(R.id.am_progressbar, false)
            if (item.lineFull_refund == 0) {
                helper.setBackgroundRes(R.id.iv_yifan, R.mipmap.dabiao)
            } else {
                helper.setBackgroundRes(R.id.iv_yifan, R.mipmap.yifan)
            }
        } else {
            helper.setVisible(R.id.iv_yifan, false).setVisible(R.id.am_progressbar, true)
            if (lineCard_amout > 10000) {
                val n = lineCard_amout as Double / 10000
                val decimalFormat = DecimalFormat("0.00")
                helper.getView<CustomCircleProgressBar>(R.id.am_progressbar)
                    .setProgressText(decimalFormat.format(n) + "万")
            } else {
                helper.getView<CustomCircleProgressBar>(R.id.am_progressbar)
                    .setProgressText(item.lineCard_amout)
            }
            helper.getView<CustomCircleProgressBar>(R.id.am_progressbar)
                .setProgress(fenmu)
        }
    }


}

private fun formatDouble(view: TextView, value: Double) {
    val decimalFormat = DecimalFormat("0.00")
    if (value >= 10000) {
        val n = value / 10000
        view.text = "¥ " + decimalFormat.format(n) + "万"
    } else {
        view.text = "¥ " + decimalFormat.format(value)
    }
}

